package com.wxb.qd.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.wxb.qd.dto.ExcelMessage;
import com.wxb.qd.entity.Aerodata;
import com.wxb.qd.service.AerodataService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class MyMessageListener {

    @Resource
    private TableListener tableListener;

    @Resource
    private AerodataService aerodataService;

    @RabbitListener(queues = "myQueue")
    public void receiveExcelPath(ExcelMessage excelMessage) {
        EasyExcel.read(excelMessage.getFilePath(),Aerodata.class,tableListener).sheet().doRead();
        List<Aerodata> aerodataList = tableListener.getAerodataList();
        for (Aerodata aerodata: aerodataList){
            String jsonString = JSON.toJSONString(aerodata);
            aerodata.setJsonData(jsonString);
            aerodata.setId(null);
            if (aerodata.getUserId()==null){
                aerodata.setUserId(Integer.parseInt(excelMessage.getId()));
            }
            aerodataService.save(aerodata);
        }
    }




}
